package com.newsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsystem.MyUtils.CommonResult;
import com.newsystem.mapper.NewsCoverMapper;
import com.newsystem.pojo.NewsCover;
import com.newsystem.service.INewsCoverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

@Service
public class NewsCoverService extends ServiceImpl<NewsCoverMapper, NewsCover> implements INewsCoverService {

    @Autowired
    private NewsCoverMapper newsCoverMapper;

//    根据获取新闻封面列表
    public CommonResult<Object> selectCoverList(Integer startPage, Integer sumPage) {
        Page<NewsCover> page = new Page<>(startPage,sumPage);
        QueryWrapper<NewsCover> queryWrapper = new QueryWrapper<NewsCover>();
        Page<NewsCover> page1 = newsCoverMapper.selectPage(page, queryWrapper);
        if(page1!=null){
            return CommonResult.success(page1);
        }
        return CommonResult.failed("首页封面抽象失败");
    }

    //todo 修改新闻封面
    public CommonResult<Object> updateNewCover(String newsId, String coverUrl,String title) {
        NewsCover newsCover = newsCoverMapper.selectByNewsId(newsId);
        if(newsCover!=null){
            if(coverUrl!=null && !coverUrl.equals(""))
                newsCover.setCoverUrl(coverUrl);
            if(title!=null && !title.equals(""))
                newsCover.setTitle(title);
            if(newsCoverMapper.updateById(newsCover) > 0){
                return CommonResult.success("修改成功");
            }
        }
        return CommonResult.failed("修改封面失败");
    }
}
